package com.hobart.learning.lagou.edu.servlet;

import com.hobart.learning.lagou.edu.annotation.Autowired;
import com.hobart.learning.lagou.edu.annotation.Component;
import com.hobart.learning.lagou.edu.factory.BeanFactory;
import com.hobart.learning.lagou.edu.factory.ProxyFactory;
import com.hobart.learning.lagou.edu.service.ITransferService;
import com.hobart.learning.lagou.edu.utils.JsonUtils;
import com.hobart.learning.lagou.edu.utils.ResultUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：hobart
 * @date ：Created in 2021-1-28 14:57
 * @description：  转账 Servlet
 * @version: 1.0
 */
@WebServlet(name = "transferServlet", urlPatterns = {"/transferServlet"})
@Component
public class TransferServlet extends HttpServlet {
    // 第一版
    // 1. 实例化service层对象
    //private ITransferService transferService = new TransferServiceImpl();

    // 第二版
    //private ITransferService transferService = (ITransferService) BeanFactory.getBean("transferService");

    //第五版
//    private ITransferService transferService = (ITransferService) ProxyFactory.getInstance().getProxy(BeanFactory.getBean("transferService"));

    //第六版
//    private ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
//    private ITransferService transferService = (ITransferService) proxyFactory.getProxy(BeanFactory.getBean("transferService"));

    private ITransferService transferService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        ServletContext servletContext = config.getServletContext();
        System.out.println("ITransferService.class.getName() = " + ITransferService.class.getName());
        transferService = (ITransferService) servletContext.getAttribute(ITransferService.class.getName());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求体的字符编码
        req.setCharacterEncoding("UTF-8");

        String fromCardNo = req.getParameter("fromCardNo");
        String toCardNo = req.getParameter("toCardNo");
        String moneyStr = req.getParameter("money");
        int money = Integer.parseInt(moneyStr);

        ResultUtils result = new ResultUtils();

        try {

            // 2. 调用service层方法
            transferService.transfer(fromCardNo,toCardNo,money);
            result.setStatus("200");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus("201");
            result.setMessage(e.toString());
        }

        // 响应
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().print(JsonUtils.object2Json(result));
    }
}
